var addCoreStockDetail={status:48};

function fillItemOptionHTML(n){
	var items = getItems(n,10) 
	var html="";
	for(var i=0;i<items.length;i++){ 
		html+='<div class="mui-input-row mui-radio"> <label>'+items[i].all+'</label> <input name="radio1" type="radio" value="'+items[i].i+'"></div>'
	}
	document.getElementById("itemOption").innerHTML=html 
	document.getElementById("itemOption").style.display="block"

}



document.getElementById('itemOption').addEventListener('tap',function(){
	setTimeout(function(){ 
		var ch=document.getElementsByName("radio1");
		for (var i = 0; i < ch.length; i++) {
			var r = ch[i];
			if (r.checked) {
				addCoreStockDetail.itemId=r.value
				var input=document.getElementById("itemName")
				input.value=r.previousElementSibling.innerHTML ;
				mui.trigger(input, 'input')
				document.getElementById("itemOption").style.display="none"
			} 
		} 
	},1) 	
}); 


	document.getElementById("itemName").addEventListener('input', function(event) { fillItemOptionHTML(this.value) }, false);
 
	var tapCard=""
	mui('#mui-content-id').on('tap', '.mui-card', function(event) {
		tapCard=this;
		mui.prompt("实际数量", '数量', this.children[0].children[0].innerHTML, ['确认', '取消'], function(e) {
			if (e.index == 0) { 
				myPost("pk/pkLess", {id:tapCard.csdId,countDb: e.value}, function(response) {
				   document.body.querySelector('#mui-content-id').innerHTML=""
					 loadData(); 
				} );
			} 
		})
		 
	});

	document.getElementById('saveButton').addEventListener('tap', function() {
		if( this.innerHTML=="新增"){
			
			document.getElementById("addDiv").style.display="block"
		    document.getElementById('saveButton').innerHTML="保存"
		}else{
			addCoreStockDetail.countDb =	document.getElementById("dbCount").value
			addCoreStockDetail.batchNo =	document.getElementById("batchNo").value 
			if(!addCoreStockDetail.boxCode){ 
				 mui.toast("托盘未找到");
				 return
			}
			if(!addCoreStockDetail.itemId){ 
				 mui.toast("请选择物料");
				 return
			}
			mui.post("pk/pkMore", addCoreStockDetail, function(response) {
				loadData();
				addCoreStockDetail={status:48};
				document.getElementById("addDiv").style.display="none"
				document.getElementById('saveButton').innerHTML="新增"
			}, "json");
			
		}
		
	 }, false);
	 
	 document.getElementById('mainDiv').addEventListener('tap', function(e) {
			document.getElementById("addDiv").style.display="none"
		 	document.getElementById('saveButton').innerHTML="新增"
	 }, false);
	 
	 function addMuiCard(innerHTML,d){
		var div = document.createElement('div');
		div.className = "mui-card";
		div.innerHTML = innerHTML
		div.csdId=d.id;
		var content = document.body.querySelector('#mui-content-id');
		content.appendChild(div)

	 }